package com.test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class T3ThreadPrint {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition  c1 = lock.newCondition();
        Condition  c2 = lock.newCondition();
        Condition  c3 = lock.newCondition();

        Thread t1 = new Thread(new Runnable() {
            String prefix = "A";
            @Override
            public void run() {
                int no=1;
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock.lock();
                while(true){
                    System.out.println(prefix+no+" "+System.currentTimeMillis()/100);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    c1.signalAll();
                    try {
                        c3.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    no++;

                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            String prefix = "B";
            @Override
            public void run() {
                int no=1;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock.lock();
                while(true){
                    try {
                        c1.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(prefix+no+" "+System.currentTimeMillis()/100);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    c2.signalAll();
                    no++;

                }
            }
        });


        Thread t3 = new Thread(new Runnable() {
            String prefix = "C";
            @Override
            public void run() {
                int no=1;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock.lock();
                while(true){
                    try {
                        c2.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(prefix+no+" "+System.currentTimeMillis()/100);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    c3.signalAll();
                    no++;

                }
            }
        });

        t1.start();
        t2.start();
        t3.start();

        try {
            t1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
